package com.luobo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.luobo.common.CommonConstants;
import com.luobo.common.CommonUtils;
import com.luobo.common.SystemConfig;
import com.luobo.dmq.entity.FeedbackReciever;
import com.luobo.dmq.entity.FeedbackRecieverExample;
import com.luobo.dmq.mapper.FeedbackRecieverMapper;
import com.luobo.msgsend.email.MailSender;
import com.luobo.msgsend.email.MailSenderPool;
import com.luobo.msgsend.http.HTTPException;
import com.luobo.msgsend.sms.SmsMultiSender;
import com.luobo.msgsend.sms.SmsMultiSenderResult;
import com.luobo.service.IFeedbackRecieverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
@Slf4j
public class FeedbackRecieverServiceImpl implements IFeedbackRecieverService {

    @Autowired
    FeedbackRecieverMapper feedbackRecieverMapper;
    @Autowired
    private SystemConfig systemConfig;

    @Override
    public List<FeedbackReciever> selectRecievers(Integer contactType) {
        FeedbackRecieverExample example = new FeedbackRecieverExample();
        example.createCriteria().andContactTypeEqualTo(contactType).andIsRecieveEqualTo(true);
        return feedbackRecieverMapper.selectByExample(example);
    }

    @Override
    public void sendEmails(String feedbackItemDo) {
        JSONObject feedbackJson = JSON.parseObject(feedbackItemDo);
        List<String> emails = feedbackRecieverMapper.selectEmails(CommonConstants.EMAILCODE);
        StringBuffer msg = new StringBuffer();
        msg.append("【问题描述】:")
                .append(feedbackJson.getString("problemDesc"))
                .append("\n")
                .append("【提单日期】：")
                .append(feedbackJson.getString("createDate"))
                .append("\n")
                .append("【联系方式】：")
                .append(feedbackJson.getString("contactNum"));

        if (CommonUtils.checkListNotBlanck(emails)) {
            for (String email : emails) {
                MailSender mailSender = new MailSender("【萝卜文学网】", email, "用户bug反馈", msg.toString());
                MailSenderPool.getInstance().sendByThread(mailSender);
            }
        }
    }

    @Override
    public void sendSms(String feedbackItemDo) {
        JSONObject feedbackJson = JSON.parseObject(feedbackItemDo);
        String problemId = feedbackJson.getString("problemId");
        // 需要发送短信的手机号码
        String[] tels = feedbackRecieverMapper.selectTels(CommonConstants.SMSCODE);
        if (tels != null && tels.length > 0) {

            int appid = systemConfig.getSmsappid(); // 1400开头
            // 短信应用SDK AppKey
            String appkey = systemConfig.getSmsappkey();
            int templateId = systemConfig.getTemplateId();
            // 签名
            String smsSign = "";
            // 指定模板ID单发短信
            try {
                String[] params = {problemId};
                SmsMultiSender msender = new SmsMultiSender(appid, appkey);
                SmsMultiSenderResult result = msender.sendWithParam("86", tels, templateId, params, smsSign, "", ""); // 签名参数未提供或者为空时，会使用默认签名发送短信
                if (!"OK".equals(result.errMsg)) {
                    log.error("发送短信失败，通道返回结果：" + result.toString());
                }
            } catch (HTTPException e) {
                // HTTP响应码错误
                log.error("发送短信失败：" + e);
            } catch (IOException e) {
                // 网络IO错误
                log.error("发送短信失败：" + e);
            }

        }

    }

}
